﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using System.Web;
using Microsoft.Office.Server.SocialData;
using System.Linq;

namespace Square.OverView.ControlTemplates.Square.OverView
{
    public partial class TopInformation : OverViewBaseControl
    {
        private string ProfileToday = "MyprofileToday";
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public string NewComments()
        {
            string strComments = string.Empty;
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager myUserProfileManager = new UserProfileManager(context);
                    //UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
                    SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
                    UserProfile myUserProfile = myUserProfileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
                    strComments = NewCommentNum(myUserProfile).Tables[0].Rows[0]["Total"].ToString();
                });
            }
            if (strComments == "" || strComments == null)
                strComments = "0";

            return strComments;
        }

        public string NewFriendRequest()
        {
            string strComments = string.Empty;

            strComments = "4";

            return strComments;
        }

        public string NewContents()
        {
            string strComments = string.Empty;

            strComments = "5";

            return strComments;
        }

        public string MyProfileToday()
        {
            string strToday = string.Empty;

            using (SPSite _site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    CurrentUserId = HttpContext.Current.User.Identity.Name;
                    SPList _list = _web.Lists[ProfileToday];
                    var AllReplyitems = from SPListItem spitem in _list.Items
                                     where spitem.Title == CurrentUserId
                                     select spitem;

                    var TodayReplyitems = from SPListItem nowspitem in _list.Items
                                          where nowspitem.Title == CurrentUserId && nowspitem["Created"].ToString().Substring(0,10) == DateTime.Now.ToShortDateString()
                                          select nowspitem;



                    strToday = TodayReplyitems.Count().ToString() + " / " + AllReplyitems.Count().ToString();
                }
            }

            return strToday;
        }
    }
}
